// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>

use ascii;
use bytes;
use fmt;
use io;
use memio;


// Encodes all characters from 0x00 to 0xff separated by \t. Invalid characters
// will not be printed. All possible accents follow the table as defined in
// the two bytes chapter at https://en.wikipedia.org/wiki/T.51/ISO/IEC_6937
fn print_t61_table(dest: io::handle) void = {
	for (let i = 0z; i < 16; i +=1 ) {
		fmt::fprintf(dest, "{:x}\t", i)!;
	};
	fmt::fprintln(dest)!;

	for (let i = 0z; i < 256; i += 1) {
		if (i % 16 == 0) {
			fmt::fprintln(dest)!;
		};
		match (t61_chardecode([i: u8])) {
		case insufficient =>
			fmt::fprint(dest, "")!;
		case invalid => void;
		case let r: rune =>
			if (i > 0xa0 || (ascii::isprint(r) && !ascii::isspace(r))) {
				fmt::fprint(dest, r)!;
			} else {
				fmt::fprintf(dest, "x{:.4x}", r: u32)!;
			};
		};

		if (i + 1 % 16 != 0) {
			fmt::fprint(dest, "\t")!;
		};
	};

	fmt::fprintln(dest)!;

	for (let i = 0xc1u8; i < 0xd0; i += 1) {
		if (i == 0xcc) continue;
		fmt::fprintf(dest, "{:.2x}\t", i)!;
		for (let j = 0x41u32; j < 0x7b; j += 1) {
			if (!ascii::isprint(j: rune)) {
				continue;
			};
			if (!(t61_chardecode([i: u8]) is insufficient)) {
				assert(false);
			};
			match (t61_chardecode([i: u8, j: u8])) {
			case let r: rune =>
				fmt::fprint(dest, r)!;
			case => void;
			};
		};
		fmt::fprintln(dest)!;
	};
};

@test fn t61encode() void = {
	let table = memio::dynamic();
	defer io::close(&table)!;
	print_t61_table(&table);
	assert(bytes::equal(t61_test_table, memio::buffer(&table)));
};

// Print this table as UTF-8, to visual check the characters.
const t61_test_table: [_]u8 = [
	0x30, 0x09, 0x31, 0x09, 0x32, 0x09, 0x33, 0x09, 0x34, 0x09, 0x35, 0x09,
	0x36, 0x09, 0x37, 0x09, 0x38, 0x09, 0x39, 0x09, 0x61, 0x09, 0x62, 0x09,
	0x63, 0x09, 0x64, 0x09, 0x65, 0x09, 0x66, 0x09, 0x0a, 0x0a, 0x09, 0x09,
	0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x78, 0x30, 0x30, 0x30,
	0x61, 0x09, 0x09, 0x78, 0x30, 0x30, 0x30, 0x63, 0x09, 0x78, 0x30, 0x30,
	0x30, 0x64, 0x09, 0x09, 0x09, 0x0a, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
	0x09, 0x09, 0x09, 0x09, 0x78, 0x30, 0x30, 0x31, 0x61, 0x09, 0x78, 0x30,
	0x30, 0x31, 0x62, 0x09, 0x09, 0x09, 0x09, 0x09, 0x0a, 0x78, 0x30, 0x30,
	0x32, 0x30, 0x09, 0x21, 0x09, 0x22, 0x09, 0x09, 0x09, 0x25, 0x09, 0x26,
	0x09, 0x27, 0x09, 0x28, 0x09, 0x29, 0x09, 0x2a, 0x09, 0x2b, 0x09, 0x2c,
	0x09, 0x2d, 0x09, 0x2e, 0x09, 0x2f, 0x09, 0x0a, 0x30, 0x09, 0x31, 0x09,
	0x32, 0x09, 0x33, 0x09, 0x34, 0x09, 0x35, 0x09, 0x36, 0x09, 0x37, 0x09,
	0x38, 0x09, 0x39, 0x09, 0x3a, 0x09, 0x3b, 0x09, 0x3c, 0x09, 0x3d, 0x09,
	0x3e, 0x09, 0x3f, 0x09, 0x0a, 0x40, 0x09, 0x41, 0x09, 0x42, 0x09, 0x43,
	0x09, 0x44, 0x09, 0x45, 0x09, 0x46, 0x09, 0x47, 0x09, 0x48, 0x09, 0x49,
	0x09, 0x4a, 0x09, 0x4b, 0x09, 0x4c, 0x09, 0x4d, 0x09, 0x4e, 0x09, 0x4f,
	0x09, 0x0a, 0x50, 0x09, 0x51, 0x09, 0x52, 0x09, 0x53, 0x09, 0x54, 0x09,
	0x55, 0x09, 0x56, 0x09, 0x57, 0x09, 0x58, 0x09, 0x59, 0x09, 0x5a, 0x09,
	0x5b, 0x09, 0x09, 0x5d, 0x09, 0x09, 0x5f, 0x09, 0x0a, 0x09, 0x61, 0x09,
	0x62, 0x09, 0x63, 0x09, 0x64, 0x09, 0x65, 0x09, 0x66, 0x09, 0x67, 0x09,
	0x68, 0x09, 0x69, 0x09, 0x6a, 0x09, 0x6b, 0x09, 0x6c, 0x09, 0x6d, 0x09,
	0x6e, 0x09, 0x6f, 0x09, 0x0a, 0x70, 0x09, 0x71, 0x09, 0x72, 0x09, 0x73,
	0x09, 0x74, 0x09, 0x75, 0x09, 0x76, 0x09, 0x77, 0x09, 0x78, 0x09, 0x79,
	0x09, 0x7a, 0x09, 0x09, 0x7c, 0x09, 0x09, 0x09, 0x09, 0x0a, 0x09, 0x09,
	0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x78, 0x30, 0x30,
	0x38, 0x62, 0x09, 0x78, 0x30, 0x30, 0x38, 0x63, 0x09, 0x09, 0x09, 0x09,
	0x0a, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
	0x78, 0x30, 0x30, 0x39, 0x62, 0x09, 0x09, 0x09, 0x09, 0x09, 0x0a, 0x78,
	0x30, 0x30, 0x61, 0x30, 0x09, 0xc2, 0xa1, 0x09, 0xc2, 0xa2, 0x09, 0xc2,
	0xa3, 0x09, 0x24, 0x09, 0xc2, 0xa5, 0x09, 0x23, 0x09, 0xc2, 0xa7, 0x09,
	0xc2, 0xa4, 0x09, 0x09, 0x09, 0xc2, 0xab, 0x09, 0x09, 0x09, 0x09, 0x09,
	0x0a, 0xc2, 0xb0, 0x09, 0xc2, 0xb1, 0x09, 0xc2, 0xb2, 0x09, 0xc2, 0xb3,
	0x09, 0xc3, 0x97, 0x09, 0xc2, 0xb5, 0x09, 0xc2, 0xb6, 0x09, 0xc2, 0xb7,
	0x09, 0xc3, 0xb7, 0x09, 0x09, 0x09, 0xc2, 0xbb, 0x09, 0xc2, 0xbc, 0x09,
	0xc2, 0xbd, 0x09, 0xc2, 0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